//
//  LOLClassViewController.m
//  LOLXXbox
//
//  Created by dllo on 16/3/2.
//  Copyright © 2016年 Grasstrio. All rights reserved.
//

#import "LOLClassViewController.h"
#import "LOLSortCollectionCell.h"
#import "LSZNetWorkTools.h"
#import "LOLSubModel.h"
#import "LOLVideoModel.h"
#import "LOLClassDetailViewController.h"
@interface LOLClassViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableDictionary *mydic;
@property (nonatomic, strong) NSString *myName;

@property (nonatomic, strong) UILabel *label;


@end

@implementation LOLClassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getData];
    [self createCollec];
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width, 30)];
}

- (void)createCollec{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((self.view.frame.size.width - 25)/ 4, (self.view.frame.size.width - 25)/ 4 + 25);
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.minimumLineSpacing = 5;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    flowLayout.headerReferenceSize =  CGSizeMake(30, 30);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 128) collectionViewLayout:flowLayout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[LOLSortCollectionCell class] forCellWithReuseIdentifier:@"cell"];
    
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerReuse"];
    
    
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    self.mydic = [NSMutableDictionary dictionary];
}

- (void)getData{
    [LSZNetWorkTools getWithUrl:@"http://box.dwstatic.com/apiVideoesNormalDuowan.php?action=c" parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id result) {
        NSArray *array = result;
        for (NSDictionary *dic in array) {
            NSMutableArray *arr = [NSMutableArray array];
            self.myName = [dic objectForKey:@"name"];
            NSArray *arr1 = [dic objectForKey:@"subCategory"];
            for (NSDictionary *dic2 in arr1) {
                LOLSubModel *subMOdel = [[LOLSubModel alloc] init];
                subMOdel.name = [dic2 objectForKey:@"name"];
                subMOdel.icon = [dic2 objectForKey:@"icon"];
                subMOdel.aaaTag = [dic2 objectForKey:@"tag"];
                [arr addObject:subMOdel];
                
             
            }
            [self.mydic setObject:arr forKey:self.myName];
            [self.collectionView reloadData];
        }
    } fail:^(NSError *error) {
        
    }];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LOLSortCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSString *str = [self.mydic.allKeys objectAtIndex:indexPath.section];
    NSArray *arr = [self.mydic objectForKey:str];
    cell.subModel = [arr objectAtIndex:indexPath.item];

    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    self.myName = [self.mydic.allKeys objectAtIndex:section];
    NSArray *arr = [self.mydic objectForKey:self.myName];

    return arr.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return self.mydic.allKeys.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerReuse" forIndexPath:indexPath];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width, 30)];
        label.font = [UIFont systemFontOfSize:20];
        [headerView addSubview:label];
        if (indexPath.section == 0) {
//            self.label.text = [self.mydic.allKeys objectAtIndex:0];
     
            label.text = [self.mydic.allKeys objectAtIndex:0];
            
            label.backgroundColor = [UIColor whiteColor];
   

        }else if (indexPath.section == 1){
//            self.label.text = [self.mydic.allKeys objectAtIndex:1];
        
            label.text = [self.mydic.allKeys objectAtIndex:1];
            
            label.backgroundColor = [UIColor whiteColor];
            
        

        }else if (indexPath.section == 2){
//            self.label.text = [self.mydic.allKeys objectAtIndex:2];
    
            label.text = [self.mydic.allKeys objectAtIndex:2];
            
            label.backgroundColor = [UIColor whiteColor];
        

        }else if (indexPath.section == 3){
//            self.label.text = [self.mydic.allKeys objectAtIndex:3];
       
            label.text = [self.mydic.allKeys objectAtIndex:3];
            
            label.backgroundColor = [UIColor whiteColor];
            
         

        }else if (indexPath.section == 4){
//            self.label.text = [self.mydic.allKeys objectAtIndex:4];
      
            label.text = [self.mydic.allKeys objectAtIndex:4];
            
            label.backgroundColor = [UIColor whiteColor];
            
     

        }
        return headerView;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    LOLClassDetailViewController *detail = [[LOLClassDetailViewController alloc] init];
    NSString *str = [self.mydic.allKeys objectAtIndex:indexPath.section];
    NSArray *arr = [self.mydic objectForKey:str];
    LOLSubModel *model = [arr objectAtIndex:indexPath.item];
    detail.subModel = model;
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
